package com.supermarket.manage.provider.ucenter.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.supermarket.manage.common.constant.RoleConstant;
import com.supermarket.manage.common.constant.StatusConstant;
import com.supermarket.manage.common.dto.ucenter.request.AddRoleParams;
import com.supermarket.manage.common.dto.ucenter.request.RoleAddPermissionParams;
import com.supermarket.manage.common.dto.ucenter.request.RoleUpdateParams;
import com.supermarket.manage.common.pojo.MyUser;
import com.supermarket.manage.common.pojo.Role;
import com.supermarket.manage.common.pojo.RolePermission;
import com.supermarket.manage.provider.api.ucenter.RoleManageService;
import com.supermarket.manage.provider.ucenter.mapper.RoleMapper;
import com.supermarket.manage.provider.ucenter.mapper.RolePermissionMapper;
import com.supermarket.manage.provider.ucenter.mapper.UserMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-02-29 18:47
 **/
@Service(version = "1.0.0")
public class RoleManageServiceImpl implements RoleManageService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public List<Role> getBaseRoleList() {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.between("id", 1L, 2L);
        return roleMapper.selectList(wrapper);
    }

    @Override
    public List<Role> getRoleListByBuyerId(String buyerId) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.in("buyer_id", RoleConstant.BASE_ROLE_BUYERID, buyerId);
        return roleMapper.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRoleById(Long roleId) {
        int i = roleMapper.deleteById(roleId);
        if (i == 1) {
            QueryWrapper<RolePermission> rpWrapper = new QueryWrapper<>();
            rpWrapper.eq("role_id", roleId);
            rolePermissionMapper.delete(rpWrapper);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Role getRoleById(Long roleId) {
        return roleMapper.selectById(roleId);
    }

    @Override
    public Boolean updateRoleById(RoleUpdateParams params) {
        Role role = new Role();
        role.setId(params.getId());
        role.setName(params.getName());
        role.setEnname(params.getEnname());
        role.setDescription(params.getDescription());
        role.setUpdated(new Date());
        return roleMapper.updateById(role) == 1;
    }

    @Override
    public Integer getRoleCountByBuyerId(String buyerId) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("buyer_id", buyerId);
        return roleMapper.selectCount(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addRole(AddRoleParams params) {
        Role role = new Role();
        role.setName(params.getName());
        role.setEnname(params.getEnname());
        role.setDescription(params.getDescription());
        role.setBuyerId(params.getBuyerId());
        role.setCreated(new Date());
        role.setUpdated(new Date());
        int i = roleMapper.insert(role);
        if (i == 1) {
            int j = rolePermissionMapper.insert(role.getId(), params.getPermissionIds());
            return j > 0;
        }
        return Boolean.FALSE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addPermissionToRole(RoleAddPermissionParams roleAddPermissionParams) {
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleAddPermissionParams.getRoleId());
        rolePermissionMapper.delete(wrapper);
        rolePermissionMapper.insert(roleAddPermissionParams.getRoleId(), roleAddPermissionParams.getPermissionIds());
        return Boolean.TRUE;
    }

    @Override
    public Integer getUsingCountByRoleId(Long roleId) {
        QueryWrapper<MyUser> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StatusConstant.USE).eq("roleId", roleId);
        return userMapper.selectCount(wrapper);
    }
}
